const express = require('express')
const router = express.Router()
const fs = require('fs')
const path = require('path')

router.get('/getBanners', (req, res) => {
  const bannersDir = path.join(__dirname, '../../db/banners')
  
  fs.readdir(bannersDir, (err, files) => {
    if (err) {
      console.error('读取banners文件夹失败:', err)
      return res.errHandle('读取banner资源失败')
    }
    
    // 过滤出jpg图片文件
    const bannerImages = files.filter(file => file.endsWith('.jpg'))
    
    // 构建banner数据数组，包含图片名称和访问路径
    const banners = bannerImages.map((image, index) => ({
      id: index + 1,
      imageUrl: `/banners/${image}`,
      imageName: image
    }))
    
    res.send({
      code: 200,
      success: true,
      message: '请求成功',
      data: banners
    })
  })
})

module.exports = router
